var app = getApp()
var common_Js = require("../../../component/common.js"), fetchApi = require("../../../utils/httpPost.js")
import Toast from '../../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl:app.getImgUrl(),
    pageIndex: 1,
    pageSize: 10,
    loading: false,
    netWork: true,
    isLoading: true,
    merList:[],
    isLogin:0,
    value1:'0',
    keyword:'',
    shop_type:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    that.setData({ 
      isLogin: isLogin,
    })
    if(options.type){
      this.data.shop_type = options.type;
    }
  },
  onShow: function () {
    this.initRequest()
  },
  initRequest(){
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      merList:[]
    })
    this.onHttpInit()
  },
  onChange(val){
    console.log('onChange',val)
    this.setData({
      searchValue: e.detail
    });
  },
  goExchange(e){
    console.log('goExchange',e)
    var mer_id = e.detail.currentTarget.dataset.item.id;
    wx.navigateTo({
      url: '/pagesMer/pages/exchange/index?mer_id='+mer_id,
    })
  },
  goProduct(e){
    console.log('goProduct',e)
    var mer_id = e.detail.currentTarget.dataset.item.id;
    wx.navigateTo({
      url: '/pages/product/list/index?mer_id='+mer_id,
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/mer/myList',{
      keyword:that.data.keyword,
      shop_type:that.data.shop_type,
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize,
    }, function (res) {
      console.log("获取我的商家列表", res)
      if(res.data.totalcount > 0){
        that.data.pageIndex = that.data.pageIndex + 1;
        var mers = res.data.data.mers;
        if(mers.length > 0){
          for (var index in mers) {
            that.data.merList.splice(that.data.merList.length, 0, mers[index]);
          }
        }
      }
      that.setData({ 
        merList: that.data.merList,
      });
      that.setData({ isLoading: false, loading: true, netWork: true })
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },function(){})
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作  下拉刷新
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      merList:[],
      netWork: false,
      isLoading: true
    })
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    this.onHttpInit()
  },
})